<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>
<head>
    <title>登录页</title>
    <%--echart线图、柱状图、饼图等各种图型库 cdn--%>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.3/echarts.min.js"></script>
    <%--jquery cdn--%>
    <script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script>
</head>
<body>

职级<select name="rankId">
    <c:forEach items="${rankList}" var="r">
        <option value="${r.id}">${r.name}</option>
    </c:forEach>
</select><br>

描述 <input name="remark"><br>




<button onclick="zxt()">点击展示折线图</button>
<button onclick="zzt()">点击展示柱状图</button>
<button onclick="excel()">点击Excel</button>

<div id="main" style="width: 500px;height: 500px"></div>


</body>

<script>




    function excel() {
        let rankId = $('[name=rankId]').val();
        let remark = $('[name=remark]').val();
        location.href = 'excel.do?rankId=' + rankId +'&remark=' + remark
    }

    function zzt() {
        let rankId = $('[name=rankId]').val();
        let remark = $('[name=remark]').val();

        $.ajax({
            url:'/zxt.do?rankId=' + rankId +'&remark=' + remark,
            success:function (res) {

                console.log(res)

                var x = []
                var y = []
                // 得到y轴的最大值

                var maxY = 0;

                for (let i = 0; i < res.length; i++) {
                    let ydata = res[i].ydata;

                    if (maxY < ydata){
                        maxY = ydata;
                    }
                }

                for (let i = 0; i < res.length; i++) {
                    let xdata = res[i].xdata;
                    x.push(xdata)

                    let ydata = res[i].ydata;

                    if (ydata == maxY){ //如果y轴是最大值  往数组里放一个红柱子
                        y.push({
                            value: ydata,
                            itemStyle: {
                                color: '#a90000'
                            }
                        })
                    }else{
                        y.push(ydata) // 普通柱子
                    }

                }

                var chartDom = document.getElementById('main');
                var myChart = echarts.init(chartDom);
                var option;

                option = {
                    xAxis: {
                        type: 'category',
                        data: x
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            data: y,
                            type: 'bar'
                        }
                    ]
                };

                option && myChart.setOption(option);



            }


        })

    }



    function zxt() {
        let rankId = $('[name=rankId]').val();
        let remark = $('[name=remark]').val();

        $.ajax({
            url:'/zxt.do?rankId=' + rankId +'&remark=' + remark,
            success:function (res) {

                console.log(res)

                var x = []
                var y = []

                for (let i = 0; i < res.length; i++) {
                    let xdata = res[i].xdata;
                    x.push(xdata)
                    let ydata = res[i].ydata;
                    y.push(ydata)
                }

                var chartDom = document.getElementById('main');
                var myChart = echarts.init(chartDom);
                var option;

                option = {
                    xAxis: {
                        type: 'category',
                        data: x
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            data: y,
                            type: 'line',
                            smooth: true
                        }
                    ]
                };

                option && myChart.setOption(option);

            }


        })



    }



</script>
</html>